Page({
  data: {
    hidden:false,
    showtc: false,
    theme: 'light',
    videoSrc: 'rtmp://177195.push.tlivecloud.com/live/test?txSecret=fe35703994c403aefe5c68693283d69c&txTime=65910630',
    mode:'RTC',
    autopush:false,
    muted:true,
    enableCamera:true,
    autoFocus:true,
    orientation:"vertical",
    beauty:0,
    whiteness:0,
    aspect:"3:4",
    minBitrate:200,
    maxBitrate:1000,
    audioQuality:"high",
    waitingImage : '',
    zoom : false,
    devicePosition :"back",
    remoteMirror:false,
    localMirror : "auto",
    audioReverbType:0,
    enableMic : true,
    enableAgc : false,
    enableAns : false,
    audioVolumeType : "auto",
    videoWidth : 360,
    videoHeight : 640,
    beautyStyle : "smooth",
    filter : "standard",
    pictureInPictureMode : [],
    bgmUrl : 'https://b.bdstatic.com/miniapp/images/yanyuan.mp3',
    bgmVolume : 0.5,
    micVolume : 0.6,
    snapshotSrc : null,


  },
  onReady() {
    this.ctx = mp.createLivePusherContext('pusher')
  },
  handleLivePusherStateChange(e) {
    console.log('live-pusher code:', e.detail.code)
  },
  handleLivePusherError(e) {
    console.error('live-pusher error:', e.detail.errMsg)
  },
  handleStart() {
    this.ctx.start({
      success: () => {
        console.log('start success')
      },
      fail: (res) => {
        console.log('start fail')
      },
      complete: (res) => {
        console.log('start complete',res)
      }
    })
  },
  handleScanQRCode() {
    mp.scanCode({
      complete: (res) => {
        const {result} = res
        this.setData({
          videoSrc: result
        })
      },
    })
  },
  handlePause() {
    this.ctx.pause({
      success: (res) => {
        console.log('pause success',res)
      },
      fail: (res) => {
        console.log('pause fail',res)
      },
      complete: (res) => {
        console.log('pause complete',res)
      }
    })
  },
  handleStop() {
    this.ctx.stop({
      success: () => {
        console.log('stop success')
      },
      fail: (res) => {
        console.log('stop fail')
      },
      complete: (res) => {
        console.log('stop complete',res)
      }
    })
  },
  handleResume() {
    this.ctx.resume({
      success: () => {
        console.log('resume success')
      },
      fail: (res) => {
        console.log('resume fail')
      },
      complete: (res) => {
        console.log('resume complete',res)
      }
    })
  },
  handleSwitchCamera() {
    this.ctx.switchCamera({
      success: () => {
        console.log('switch camera success')
      },
      fail: (res) => {
        console.log('switch camera fail')
      },
      complete: (res) => {
        console.log('switch camera complete',res)
      }
    })
  },
  handleVideoSrcInput(e) {
    this.setData({
      videoSrc: e.detail.value
    })
  },
  onUnload() {
    if (mp.offThemeChange) {
      mp.offThemeChange()
    }
  },
  onLoad() {
    this.setData({
      theme: mp.getSystemInfoSync().theme || 'light'
    })

    if (mp.onThemeChange) {
      mp.onThemeChange(({theme}) => {
        this.setData({theme})
      })
    }
  },
      //常规参数设置 
      setParams(e) {
        const { key } =  e.currentTarget.dataset
        var tempParams = null
        if(parseInt(e.detail.value)){
          tempParams =  parseInt(e.detail.value)
        }
        else if(e.detail.value == "null"){
          tempParams = null
        }
        else if (e.detail.value == "true" || e.detail.value == "false"){
          tempParams = JSON.parse(e.detail.value)
        }
        else if (e.detail.value.search(/{/)==0 && e.detail.value.search(/}/)==e.detail.value.length-1) {
          tempParams = eval("(" + e.detail.value + ")")
        }
        else{
          tempParams = e.detail.value
        }
        this.setData({
            [key]: tempParams
        })
        console.log(key," value:",this.data.param,",",key," type:",typeof tempParams)
      },
          // 文件路径
    setFilePath(e){
      const { key } =  e.currentTarget.dataset
      if(e.detail.value =="tempFilePath"){
        mp.downloadFile({
          url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png', 
          success: res => {
            console.log('downloadFile success, res is', res)
            this.setData({
              filePath: res.tempFilePath
            })
          }
        })
      }
      else if(e.detail.value =="storeFilePath"){
        mp.downloadFile({
          url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png', 
          success: res => {
            console.log('downloadFile success, res is', res)
            mp.saveFile({
              tempFilePath: res.tempFilePath,
              complete:(res) =>{
                console.log("mpSaveFile:",res)
                this.setData({
                filePath: res.savedFilePath
                })
              }
            })
          }
        })
      }
      else if(e.detail.value =="usrFilePath"){
        mp.downloadFile({
          url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png', // 文件资源
          filePath:`${mp.env.USER_DATA_PATH}/image.png`,
          success:(res) => {
            console.log('downloadFile success, res is', res)
            this.setData({
              filePath: res.filePath
            })
          }
        })
      }
      else{
      this.setData({
        [key]: e.detail.value
      })
      }
    },
    eventLog(e) {
      console.log('事件触发', e.type, e.detail)
      console.log(e)
    },
    exitPictureInPicture(){
      this.ctx.exitPictureInPicture({
        success: (res) => {
          console.log('exitPictureInPicture success',res)
        },
        fail: (res) => {
          console.log('exitPictureInPicture fail',res)
        },
        complete: (res) => {
          console.log('exitPictureInPicture complete',res)
        }
      })
    },
    pauseBGM() {
      this.ctx.pauseBGM({
        success: (res) => {
          console.log('pauseBGM success',res)
        },
        fail: (res) => {
          console.log('pauseBGM fail',res)
        },
        complete: (res) => {
          console.log('pauseBGM complete',res)
        }
      })
    },
    playBGM() {
      this.ctx.playBGM({
        url : this.data.bgmUrl,
        success: (res) => {
          console.log('pauseBGM success',res)
        },
        fail: (res) => {
          console.log('pauseBGM fail',res)
        },
        complete: (res) => {
          console.log('pauseBGM complete',res)
        }
      })
    },
          // set bgmUrl
          setBgmUrl(e){
            const { key } =  e.currentTarget.dataset
            if(e.detail.value =="tempFilePath"){
              mp.downloadFile({
                url: 'https://b.bdstatic.com/miniapp/images/yanyuan.mp3', 
                success: res => {
                  console.log('downloadFile success, res is', res)
                  this.setData({
                    bgmUrl: res.tempFilePath
                  })
                }
              })
            }
            else if(e.detail.value =="storeFilePath"){
              mp.downloadFile({
                url: 'https://b.bdstatic.com/miniapp/images/yanyuan.mp3', 
                success: res => {
                  console.log('downloadFile success, res is', res)
                  mp.saveFile({
                    tempFilePath: res.tempFilePath,
                    complete:(res) =>{
                      console.log("mpSaveFile:",res)
                      this.setData({
                        bgmUrl: res.savedFilePath
                      })
                    }
                  })
                }
              })
            }
            else if(e.detail.value =="usrFilePath"){
              mp.downloadFile({
                url: 'https://b.bdstatic.com/miniapp/images/yanyuan.mp3', // 文件资源
                filePath:`${mp.env.USER_DATA_PATH}/image.png`,
                success:(res) => {
                  console.log('downloadFile success, res is', res)
                  this.setData({
                    bgmUrl: res.filePath
                  })
                }
              })
            }
            else{
            this.setData({
              [key]: e.detail.value
            })
            }
          },
          resumeBGM() {
            this.ctx.resumeBGM({
              success: (res) => {
                console.log('resumeBGM success',res)
              },
              fail: (res) => {
                console.log('resumeBGM fail',res)
              },
              complete: (res) => {
                console.log('resumeBGM complete',res)
              }
            })
          },
          stopBGM() {
            this.ctx.stopBGM({
              success: (res) => {
                console.log('stopBGM success',res)
              },
              fail: (res) => {
                console.log('stopBGM fail',res)
              },
              complete: (res) => {
                console.log('stopBGM complete',res)
              }
            })
          },
          sendMessage() {
            this.ctx.sendMessage({
              msg:"sendMessage abcdefg......",
              success: (res) => {
                console.log('sendMessage success',res)
              },
              fail: (res) => {
                console.log('sendMessage fail',res)
              },
              complete: (res) => {
                console.log('sendMessage complete',res)
              }
            })
          }, 
          setBGMVolume() {
            this.ctx.setBGMVolume({
              volume:this.data.bgmVolume,
              success: (res) => {
                console.log('setBGMVolume success',res)
              },
              fail: (res) => {
                console.log('setBGMVolume fail',res)
              },
              complete: (res) => {
                console.log('setBGMVolume complete',res)
              }
            })
          }, 
          setMICVolume() {
            this.ctx.setMICVolume({
              volume:this.data.micVolume,
              success: (res) => {
                console.log('setMICVolume success',res)
              },
              fail: (res) => {
                console.log('setMICVolume fail',res)
              },
              complete: (res) => {
                console.log('setMICVolume complete',res)
              }
            })
          }, 
  
  snapshot() {
    this.ctx.snapshot({
      quality:'raw',  //raw：原图、compressed：压缩图
      sourceType : "stream",  //stream：截取视频源、view：截取渲染后的画面
      success: (res) => {
        console.log('snapshot success',res)
        this.setData({
          snapshotSrc: res.tempImagePath
        })
      },
      fail: (res) => {
        console.log('snapshot fail',res)
      },
      complete: (res) => {
        console.log('snapshot complete',res)
      }
    })
  }, 
  startPreview() {
    this.ctx.startPreview({
      success: (res) => {
        console.log('startPreview success',res)
      },
      fail: (res) => {
        console.log('startPreview fail',res)
      },
      complete: (res) => {
        console.log('startPreview complete',res)
      }
    })
  }, 
  stopPreview() {
    this.ctx.stopPreview({
      success: (res) => {
        console.log('stopPreview success',res)
      },
      fail: (res) => {
        console.log('stopPreview fail',res)
      },
      complete: (res) => {
        console.log('stopPreview complete',res)
      }
    })
  }, 
  toggleTorch() {
    this.ctx.toggleTorch({
      success: (res) => {
        console.log('toggleTorch success',res)
      },
      fail: (res) => {
        console.log('toggleTorch fail',res)
      },
      complete: (res) => {
        console.log('toggleTorch complete',res)
      }
    })
  }, 
  
  tcxrClick() {
    this.setData({
      showtc: true
    })
  }
})
